<?php
/**
 * User: Hufeng
 * Date: 2017/12/04 17:51
 * Desc: TRADE 商城SERVER
 */
namespace App\Controller;
use App\Model\GoodsModel;
use App\Model\GoodsPicModel;
use App\Model\OrderModel;
use App\Common\Util\Redis;
use App\Model\OrderExtModel;
use App\Model\UserAddressModel;
use App\Model\UserAssetsModel;
use App\Model\UserModel;
use App\Model\UserAssetsLogModel;

class Shop extends Base {

    public function __construct(){
        parent::__construct();
    }

    //商品列表
    public function goodsList(){
        $gModel = new GoodsModel();
        $gQuery = $gModel->queryData(['status'=>1],['id','name','need_money','need_coin']);
        $gidArr = [];
        foreach ($gQuery as $val){
            $gidArr[] = $val['id'];
        }
        $gpicArr = [];
        if($gidArr){
            $gpModel = new GoodsPicModel();
            $gpQuery = $gpModel->queryData(['gid'=>$gidArr,'type'=>1,'ORDER'=>['id'=>'DESC']]);
            foreach ($gpQuery as $val){
                $gpicArr[$val['gid']] = $val['pic'];
            }
        }
        $data = [];
        foreach ($gQuery as $val){
            $arr = [];
            $arr['id'] = $val['id'];
            $arr['name'] = $val['name'];
            $arr['money'] = $val['need_money'];
            $arr['coin'] = $val['need_coin'];
            $arr['pic'] = $gpicArr[$val['id']];
            $data[] = $arr;
        }
        $this->outData['data'] = $data;
        return $this->fjson($this->outData);
    }
    //商品详情页
    public function goodsInfo(){
        $needParam = array(
            'gid'=>array('msg'=>'gid参数异常','type'=>'int')
        );
        $this->regArguments($needParam,$this->param);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $gModel = new GoodsModel();
        $gRes = $gModel->findData(['id'=>$this->param['gid'],'status'=>1]);
        $navImg = [];
        $infoImg = [];
        if($gRes){
            $gpModel = new GoodsPicModel();
            $gpList = $gpModel->queryData(['gid'=>$this->param['gid']]);
            foreach ($gpList as $val){
                if($val['type'] == 1){
                    $navImg[] = $val['pic'];
                }else{
                    $infoImg[] = $val['pic'];
                }
            }
        }
        $goodsRes['id'] = $gRes['id'];
        $goodsRes['name'] = $gRes['name'];
        $goodsRes['money'] = $gRes['need_money'];
        $goodsRes['coin'] = $gRes['need_coin'];
        $goodsRes['info'] = $gRes['info'];
        $goodsRes['navImg'] = $navImg;
        $goodsRes['infoImg'] = $infoImg;
        $this->outData['data'] = $goodsRes;
        return $this->fjson($this->outData);
    }
    //创建订单
    public function buildOrder(){
        $needParam = array(
            'openId'=>array('msg'=>'openId参数异常','type'=>'str'),
            'gid'=>array('msg'=>'gid参数异常','type'=>'int'),
            'aid'=>array('msg'=>'aid参数异常','type'=>'int'),
            'token'=>array('msg'=>'token参数异常','type'=>'str')
        );
        $this->regArguments($needParam,$this->param);
        $this->regToken($this->param['token'],$this->param['openId']);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $uModel = new UserModel();
        $uid = $uModel->getUidByOpenId($this->param['openId']);
        if(!$uid){
            return $this->fjson(['code'=>1,'msg'=>'账号异常，请联系客服人员']);
        }

        $this->redis = new Redis();
        if(!$this->redis->getLock(USER_BALANCE_RKEY.$uid)){
            return json_encode(['code'=>1,'msg'=>'请求未成功，请重试']);
        };

        //商品价格
        $gModel = new GoodsModel();
        $gRes = $gModel->findData(['id'=>$this->param['gid'],'status'=>1]);
        if(!$gRes['id']){
            return $this->fjson(['code'=>1,'msg'=>'商品已下架']);
        }
        $goodsCoin = $gRes['need_coin'];
        $goodsMoney = $gRes['need_money'];
        $uaModel = new UserAssetsModel();
        $resBool = $uaModel->reduceAssets($uid,$goodsCoin,BALANCE_COIN,BALANCE_ENABLE,'兑换商品');
        if(!$resBool){
            return $this->fjson(['code'=>1,'msg'=>'金币不足，无法兑换']);
        }

        //创建订单
        $status = $goodsMoney>0 ? 1 : 2;
        $oModel = new OrderModel();
        $oData = [
            'uid'=> $uid,
            'order_no'=> $this->buildOrderNo(),
            'order_type'=> ORDER_TYPE_SHOP,
            'pay_money'=> $goodsMoney,
            'pay_coin'=> $goodsCoin,
            'status'=> $status,
            'addtime'=> $this->timeNow,
        ];
        $status == 2 ? $oData['paytime']=$this->timeNow : '';
        $oid = $oModel->insertData($oData);

        $uaModel = new UserAddressModel();
        $uaRes = $uaModel->findData(['id'=>$this->param['aid']]);
        $gpModel = new GoodsPicModel();
        $goodsSku = ['gname'=>$gRes['name'],'gpic'=>$gpModel->getOnePic($this->param['gid'])];
        $rpcSku = ['uname'=>$uaRes['uname'],'tel'=>$uaRes['tel'],'address'=>$uaRes['pname'].$uaRes['cnmae'].$uaRes['aname'].$uaRes['info']];

        //订单扩展表
        $oeModel = new OrderExtModel();
        $oeData = [
            'oid'=>$oid,
            'goods_sku'=>json_encode($goodsSku,JSON_UNESCAPED_UNICODE),
            'rcp_sku'=>json_encode($rpcSku,JSON_UNESCAPED_UNICODE)
        ];
        $oeModel->insertData($oeData);
        $this->outData['orderNo'] = $oData['order_no'];
//        $this->outData['status'] = $status;
        $this->redis->removeLock(USER_BALANCE_RKEY.$uid);
        return $this->fjson($this->outData);
    }
    //商品兑换记录(商品图片，商品名称，订单号，金额，时间)
    public function myOrder(){
        $needParam = array(
            'openId'=>array('msg'=>'openId参数异常','type'=>'str')
        );
        $this->regArguments($needParam,$this->param);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $uModel = new UserModel();
        $uid = $uModel->getUidByOpenId($this->param['openId']);
        if(!$uid){
            return $this->fjson(['code'=>1,'msg'=>'账号异常，请联系客服人员']);
        }
        $oModel = new OrderModel();
        $oList = $oModel->queryData(['uid'=>$uid,'ORDER'=>['id'=>'DESC']]);
        $oidArr = [];
        foreach ($oList as $val){
            $oidArr[] = $val['id'];
        }
        $oeData = [];
        if($oidArr){
            $oeModel = new OrderExtModel();
            $oeList = $oeModel->queryData(['oid'=>$oidArr]);
            foreach ($oeList as $val){
                $goodsSku = json_decode($val['goods_sku'],true);
                $oeData[$val['oid']] = $goodsSku;
            }
        }
        $data = [];
        foreach ($oList as $val){
            $arr = [];
            $arr['oid'] = $val['id'];
            $arr['orderNo'] = $val['order_no'];
            $arr['payCoin'] = $val['pay_coin'];
            $arr['addtime'] = $val['addtime'];
            $arr['gname'] = $oeData[$val['id']]['gname'];
            $arr['gpic'] = $oeData[$val['id']]['gpic'];
            $data[] = $arr;
        }
        $this->outData['data'] = $data;
        return $this->fjson($this->outData);
    }
}
